<template>
	<view>
		<topbar title="登录"></topbar>
		<view class="content">
			<view class="dis_f_co_c_c" style="width: 100%;">
				<view class="login dis_f_c_c" style="margin-bottom: 42rpx;">
					<image src="../../static/login/zh.png" mode=""></image>
					<u-input type="number" v-model="account" placeholder="请输入手机号" maxlength="11"></u-input>
				</view>
				<view class="login dis_f_c_c" style="margin-bottom: 144rpx;">
					<image src="../../static/login/mm.png" mode=""></image>
					<u-input type="password" v-model="password"  placeholder="请输入密码" maxlength="16"></u-input>
				</view>
				<!-- <view class="dis_f hh" style="width: 100%;">{{res}}</view> -->
				<view class="btn dis_f_c_c" @click='login'>
					登录
				</view>
				<view class="btn dis_f_c_c" @click='register' style="margin-top: 50rpx;">
					免密登录
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account:'',
				password:'',
				timer:null,
			};
		},
		onUnload() {
			if(this.timer) {  
				clearTimeout(this.timer);  
				this.timer = null;  
			}  
		},
		methods:{
			login(){
				if(this.account == ''){
					this.$.ti_shi('请输入手机号')
				}
				if(this.password == ''){
					this.$.ti_shi('请输入密码')
				}
				this.$.ajax(1, 'post', 'index/login', {
					phone: this.account,
					password:this.password,
				}, (res) => {
					
					if(res.code == 1){
						this.$.ti_shi(res.msg)
						uni.setStorageSync('uid',res.uid);
						uni.setStorageSync('token',res.token);
						let promise = this.tim.login({
							userID: res.uid.toString(),
							userSig: res.sign
						});
						promise.then((res) => {
							//登录成功后 更新登录状态
							this.$store.commit("toggleIsLogin", true);
							//自己平台的用户基础信息
							// uni.setStorageSync('userInfo', JSON.stringify(userInfo))
							//tim 返回的用户信息
							uni.setStorageSync('userTIMInfo', JSON.stringify(res.data))
							console.log(res.data)
							
						}).catch((err) => {
							console.warn('login error:', err); // 登录失败的相关信息
						});
						this.timer = setTimeout( () => {
						    uni.switchTab({
						    	url:'/pages/index/index'
						    })
						}, 1000)
						
					}else{
						this.$.ti_shi(res.msg)
					}
					
				})
			},
			register(){
				this.$.open('/pages/login/mianlogin')
			},
			// 返回
			go_back() {
				this.$.back()
			},
		}
	}
</script>

<style scoped lang="scss">
	page{
		background-color: #FFF !important;
	}
	
	.content{
		width: 100%;
		padding: 0 64rpx;
		top: 240rpx;
		z-index: 998;
		position: absolute;
		.login{
			width: 100%;
			height: 100rpx;
			background-color: #F6F6F6;
			border-radius: 20rpx;
			padding: 30rpx 36rpx;
			image{
				width: 28.25rpx;
				height: 37.82rpx;
				margin-right: 15.75rpx;
			}
		}
		.btn{
			width: 100%;
			height: 92rpx;
			background-color: #E23C63;
			border-radius: 46rpx;
			color: #FFF;
			font-size: 32rpx;
		}
	}
</style>
